package com.msmall.base.rms.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.StatementType;
import org.springframework.stereotype.Repository;

import com.msmall.base.mapper.SqlMapper;
import com.msmall.base.rms.po.Administer;
import com.msmall.base.rms.po.AdministerGroup;

@Repository("administerMapper")
public interface AdministerMapper extends SqlMapper {

	@Insert("INSERT INTO t_rms_administer(username,password) "
			+ "VALUES(#{administer.username},#{administer.password})")
	@SelectKey(before = false, keyProperty = "administer.id", resultType = Integer.class, statementType = StatementType.STATEMENT, statement = "SELECT LAST_INSERT_ID() AS id")
	public void insertAdminister(final @Param("administer") Administer administer);

	@Update("UPDATE t_rms_administer SET username=#{administer.username} WHERE id=#{administer.id}")
	public void updateAdminister(@Param("administer") final Administer administer);
	
	@Update("UPDATE t_rms_administer SET password=#{administer.password} WHERE id=#{administer.id}")
	public void updateAdministerPassword(@Param("administer") final Administer administer);
	
	public Administer selectAdministerByid(@Param("id") Integer id);

	public List<Administer> selectAdministerByUsername(@Param("username") String username);
	
	@Delete("DELETE FROM t_rms_administer WHERE id=#{id}")
	public void deleteAdminister(final Integer id);
	
	@Delete("DELETE FROM t_rms_administer_group WHERE administer_id=#{administerid}")
	public void deleteAdministerGroupByAdminister(final Integer administerid);
	
	@Delete("DELETE FROM t_rms_administer_group WHERE id=#{id}")
	public void deleteAdministerGroup(final Integer id);
	
	public Integer findAdministerPageCount(@Param("username") String username);
	
	public List<Administer> findAdministerPage(@Param("username") String username, @Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize);
//	@Lang(RawLanguageDriver.class)
//	@Select("SELECT g.* FROM t_rms_group g, t_rms_administer_group ag WHERE g.id=ag.group_id AND ag.administer_id=#{administerid}")
//	public List<Group> selectGroupByAdminister(@Param("administerid") final Integer administerid);
	
	@Insert("INSERT INTO t_rms_administer_group(group_id,administer_id) "
			+ "VALUES(#{ag.groupid},#{ag.administer_id}")
	@SelectKey(before = false, keyProperty = "ag.id", resultType = Integer.class, statementType = StatementType.STATEMENT, statement = "SELECT LAST_INSERT_ID() AS id")
	public AdministerGroup insertAdministerGroup( final @Param("ag") AdministerGroup administerGroup);
	
	@Delete("DELETE FROM t_rms_administer_group WHERE administer_id=#{administerid} AND group_id=#{groupid}")
	public void deleteAdministerGroup(final @Param("administerid") Integer administerid, final @Param("groupid") Integer groupid);
	
}
